/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.base.thread
 * author:David
 * Date:2015年8月20日上午9:29:44
 * Copyright (c) 2015, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.base.thread;

/**
 * 多个线程之间共享数据的方式探讨
 * @author David
 * @since 2015年8月20日 上午9:29:44
 * @version 
 * @since JDK 1.6
 */
public class MultiThreadShareData {
//	private static ShareData1 data1=new ShareData1();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ShareData1 data2=new ShareData1();
		new Thread(new MyRunnable1(data2)).start();
		new Thread(new MyRunnable2(data2)).start();
		
		final ShareData1 data1=new ShareData1();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				data1.increment();			
			}
		}).start();
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				data1.decrement();
			}
		}).start();
		
		
		
	}

}

class MyRunnable1 implements Runnable{
	private ShareData1 data1;
	public MyRunnable1(ShareData1 data1){
		this.data1=data1;
	}

	@Override
	public void run() {
		data1.decrement();
	}
	
}

class MyRunnable2 implements Runnable{
	private ShareData1 data1;
	public MyRunnable2(ShareData1 data1){
		this.data1=data1;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		data1.increment();
	}
	
}


class ShareData1 implements Runnable{
	private int j=0;
	public synchronized void increment(){
		j++;
	}
	
	public synchronized void decrement(){
		j--;
	}

	private int count=100;
	@Override
	public void run() {
		while(true){
			count--;
		}
		
	}
	
}